<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/20
 * Time: 17:32
 */

namespace common\sources\read\buyuser;

use common\models\craftsman\BuyUserAccount;

class BuyUserAcountAccess implements IBuyUserAcountAccess
{
    public function allBuyUserAccount($ids) {
        return BuyUserAccount::find()
            ->alias('b')
            ->select("b.buy_id,b.remail_golds,b.remain_funds,b.freezing_funds,b.freezing_golds,u.phone,u.role")
            ->leftJoin('buyuser u','u.id = b.buy_id')
            ->where(['buy_id' => $ids])
            ->indexBy('buy_id')
            ->asArray()
            ->all();
    }

    public function getBuyUserAccount($buyId)
    {
        return BuyUserAccount::find()
            ->select('buy_id,remain_funds,remail_golds,freezing_funds,freezing_golds,income_golds')
            ->where(['buy_id' => $buyId])
            ->asArray()
            ->one();
    }

    //获取用户资金排行榜
    public function orderUserBank($data)
    {
        $result = BuyUserAccount::find()
            ->select("phone,remain_funds,remail_golds,freezing_funds,freezing_golds,income_golds")
            ->leftJoin('buyuser','buyuser.id = buyuser_account.buy_id')
            ->where("buyuser.role = " . $data['role'])
            ->limit($data['limit'])
            ->orderBy("remain_funds desc")
            ->asArray()
            ->all();
        return $result;
    }

    public function RechargeList($data,$count = false) {
        $query = BuyUserAccount::find()
            ->alias('b')
            ->select("b.order_id,b.amount,b.create_time,b.status,b.rech_type,b.pay_type,
            u.phone,u.role")
            ->leftJoin("buyuser u",'u.id = b.buy_id')
            ->where(['rech_type' => $data['change'],'status' => 2]);
        if (!empty($data['phone'])) {
            $query = $query->andWhere(['u.phone' => $data['phone']]);
        }
        if (!empty($data['order_id'])) {
            $query = $query->andWhere(['b.order_id' => $data['order_id']]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'create_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'create_time',
                $data['end_time']
            ]);
        }
        if ($count) {
            $count = $query->asArray()->count();
            return $count;
        }
        $list = $query->offset(($data['page']-1)*$data['limit'])
            ->limit($data['limit'])
            ->asArray()
            ->all();
        return $list;
    }

    public function getPepper($buy_id) {
        $userAmount = BuyUserAccount::find()
            ->select("remail_golds,remain_funds")
            ->where(['buy_id' => $buy_id])
            ->one();
        return $userAmount;
    }
}